<!--
 * new page
 * @author: liuxin
 * @since: 2024-08-16
 * delete_admin.vue
-->
<template>
	<el-dialog v-model="dialogFormVisible" title="删除操作" width="30%" center>
		<span v-if="adminid">是否去掉此用户的管理员职位？删除后此用户将重新展现在用户列表中</span>
		<span v-else>请谨慎操作，删除后此用户将永久失去登录资格</span>
		<template #footer>
			<span class="dialog-footer">
				<el-button type="primary" @click="deleteAdmin">确定</el-button>
			</span>
		</template>
	</el-dialog>
</template>

<script setup lang="ts">
import { ref, onBeforeUnmount } from "vue"
import { bus } from "@/utils/mitt.js"
import { changeIdentityToUser, deleteUser } from '@/api/userinfor.js'
import { tracking } from '@/utils/operation.js'
import { ElMessage } from 'element-plus'

const adminid = ref()
const userid = ref()
const account = ref()
const name = ref()
bus.on('deleteId', (id: number) => {
	adminid.value = id
})
bus.on('deleteUserId', (userInfor: any) => {
	userid.value = userInfor.id
	account.value = userInfor.account
	name.value = userInfor.name

})
const emit = defineEmits(['success'])
const deleteAdmin = async () => {
	if (adminid.value) {
		const res = await changeIdentityToUser(adminid.value)
		if (res.status == 0) {
			ElMessage({
				message: '对管理员降职成功',
				type: 'success'
			})
			// emit('success')
			bus.emit('adminDialogOff', 3)
			dialogFormVisible.value = false
		} else {
			ElMessage.error('对管理员降职失败')
			dialogFormVisible.value = false
		}
	}
	if (userid.value) {
		const res = await deleteUser(userid.value, account.value)
		if (res.status == 0) {
			ElMessage({
				message: '删除用户成功',
				type: 'success'
			})
			tracking('管理员', localStorage.getItem('name'), name.value, '高级')
			// emit('success')
			/*   
			假如用户在第二页 且是第一条数据 删除之后 页面变为第一页
			假如用户在第二页 不是第一条数据 删除之后 页面依然是第二页 
		    
			*/
			bus.emit('adminDialogOff', 1)
			dialogFormVisible.value = false
			bus.emit('offDialog', 1)
		} else {
			ElMessage.error('删除用户失败')
			dialogFormVisible.value = false
		}
	}

}
// 弹窗开关

const dialogFormVisible = ref(false)
// 打开编辑管理员弹窗
const open = () => {
	dialogFormVisible.value = true
}
defineExpose({
	open
})

// 取消订阅/监听
onBeforeUnmount(() => {
	bus.all.clear()
})
</script>

<style scoped lang="scss"></style>